package uit.qlks.from;

import javax.sql.rowset.WebRowSet;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import uit.ejb.dto.UserDTO;
import uit.qlks.controller.user.UserController;

/**
 * 
 * @author SVN - Team
 */
public class frmDoimatkhau extends javax.swing.JFrame {

    /** Creates new form Form_ThongTin */
    public frmDoimatkhau() {
        initComponents();
        this.setResizable(false);
    }
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel_TK_fThongTin = new javax.swing.JLabel();
        jLabel_Quyen_fThongTin = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jRB_Them_fNguoiDung = new javax.swing.JRadioButton();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jPF_MKCu_fThongTin = new javax.swing.JPasswordField();
        jPF_MKMoi_fThongTin = new javax.swing.JPasswordField();
        jbcapnhat = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();
        jPF_MKMoi2_fThongTin = new javax.swing.JPasswordField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Đổi Mật Khẩu");
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                formComponentShown(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Thông tin", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Times New Roman", 0, 18))); // NOI18N

        jLabel_TK_fThongTin.setFont(new java.awt.Font("Times New Roman", 0, 18));
        jLabel_TK_fThongTin.setText("Tài khoản");

        jLabel_Quyen_fThongTin.setFont(new java.awt.Font("Times New Roman", 0, 18));
        jLabel_Quyen_fThongTin.setText("Quyền");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jLabel_Quyen_fThongTin, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel_TK_fThongTin, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 354, Short.MAX_VALUE)))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel_TK_fThongTin)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel_Quyen_fThongTin))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Times New Roman", 0, 18))); // NOI18N

        jRB_Them_fNguoiDung.setFont(new java.awt.Font("Times New Roman", 0, 18));
        jRB_Them_fNguoiDung.setText("Đổi mật khẩu");
        jRB_Them_fNguoiDung.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRB_Them_fNguoiDungActionPerformed(evt);
            }
        });

        jLabel5.setFont(new java.awt.Font("Times New Roman", 0, 16));
        jLabel5.setText("Mật khẩu cũ:");

        jLabel6.setFont(new java.awt.Font("Times New Roman", 0, 16));
        jLabel6.setText("Mật khẩu mới:");

        jPF_MKCu_fThongTin.setFont(new java.awt.Font("Times New Roman", 0, 16));
        jPF_MKCu_fThongTin.setEnabled(false);
        jPF_MKCu_fThongTin.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jPF_MKCu_fThongTinKeyTyped(evt);
            }
        });

        jPF_MKMoi_fThongTin.setFont(new java.awt.Font("Times New Roman", 0, 16));
        jPF_MKMoi_fThongTin.setEnabled(false);
        jPF_MKMoi_fThongTin.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jPF_MKMoi_fThongTinKeyTyped(evt);
            }
        });

        jbcapnhat.setFont(new java.awt.Font("Times New Roman", 0, 16));
        jbcapnhat.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/Edit.png"))); // NOI18N
        jbcapnhat.setText("Cập nhật");
        jbcapnhat.setBorder(null);
        jbcapnhat.setContentAreaFilled(false);
        jbcapnhat.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/Edit2.png"))); // NOI18N
        jbcapnhat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbcapnhatActionPerformed(evt);
            }
        });

        jLabel7.setFont(new java.awt.Font("Times New Roman", 0, 16));
        jLabel7.setText("Nhập lại mật khẩu:");

        jPF_MKMoi2_fThongTin.setFont(new java.awt.Font("Times New Roman", 0, 16));
        jPF_MKMoi2_fThongTin.setEnabled(false);
        jPF_MKMoi2_fThongTin.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jPF_MKMoi2_fThongTinKeyTyped(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jRB_Them_fNguoiDung)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jPF_MKCu_fThongTin)
                            .addComponent(jPF_MKMoi_fThongTin, javax.swing.GroupLayout.DEFAULT_SIZE, 151, Short.MAX_VALUE)))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel7)
                        .addGap(15, 15, 15)
                        .addComponent(jPF_MKMoi2_fThongTin, javax.swing.GroupLayout.DEFAULT_SIZE, 149, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jbcapnhat)
                        .addContainerGap())))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jRB_Them_fNguoiDung)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jPF_MKCu_fThongTin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(jPF_MKMoi_fThongTin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(jbcapnhat)
                    .addComponent(jPF_MKMoi2_fThongTin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(14, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(31, Short.MAX_VALUE))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-434)/2, (screenSize.height-350)/2, 434, 350);
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
        // TODO add your handling code here:
        frmMain._main.setVisible(true);
        frmMain._main.setFocusable(true);
    }//GEN-LAST:event_formWindowClosed

    private void jPF_MKCu_fThongTinKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jPF_MKCu_fThongTinKeyTyped
        // TODO add your handling code here:
        if (jPF_MKCu_fThongTin.getPassword().length == 10) {
            getToolkit().beep();
            evt.consume();
        }
        if (!Character.isLetterOrDigit(evt.getKeyChar()) && !Character.isISOControl(evt.getKeyChar())) {
            getToolkit().beep();
            evt.consume();
        }
    }//GEN-LAST:event_jPF_MKCu_fThongTinKeyTyped

    private void jPF_MKMoi_fThongTinKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jPF_MKMoi_fThongTinKeyTyped
        // TODO add your handling code here:
        if (jPF_MKMoi_fThongTin.getPassword().length == 10) {
            getToolkit().beep();
            evt.consume();
        }
        if (!Character.isLetterOrDigit(evt.getKeyChar()) && !Character.isISOControl(evt.getKeyChar())) {
            getToolkit().beep();
            evt.consume();
        }
    }//GEN-LAST:event_jPF_MKMoi_fThongTinKeyTyped

    private void jRB_Them_fNguoiDungActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRB_Them_fNguoiDungActionPerformed
        // TODO add your handling code here:
        if (jRB_Them_fNguoiDung.isSelected()) {
            jPF_MKMoi_fThongTin.setEnabled(true);
            jPF_MKCu_fThongTin.setEnabled(true);
            jbcapnhat.setEnabled(true);
            jPF_MKMoi2_fThongTin.setEnabled(true);
        } else {
            jPF_MKMoi_fThongTin.setEnabled(false);
            jPF_MKCu_fThongTin.setEnabled(false);
            jbcapnhat.setEnabled(false);
            jPF_MKMoi2_fThongTin.setEnabled(false);
        }
    }//GEN-LAST:event_jRB_Them_fNguoiDungActionPerformed

    private void formComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentShown
        // TODO add your handling code here:

        jRB_Them_fNguoiDung.setSelected(false);
        jLabel_TK_fThongTin.setText("Tài khoản: " + frmLogin._us.getUserName());

        if (Integer.parseInt(frmLogin._us.getRole()) == 1) {
            jLabel_Quyen_fThongTin.setText("Quyền: " + "Nhân viên");
            return;
        }
        if (Integer.parseInt(frmLogin._us.getRole()) == 2) {
            jLabel_Quyen_fThongTin.setText("Quyền: " + "Quản trị");
            return;
        }
    }//GEN-LAST:event_formComponentShown

    private void jbcapnhatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbcapnhatActionPerformed
        // TODO add your handling code here:
        if ("".equals(jPF_MKCu_fThongTin.getText())) {
            JOptionPane.showMessageDialog(this, "Bạn chưa nhập mật khẩu", "Thông báo", JOptionPane.ERROR_MESSAGE);
            jPF_MKCu_fThongTin.requestFocusInWindow();
            return;
        }
        if ("".equals(jPF_MKMoi_fThongTin.getText())) {
            JOptionPane.showMessageDialog(this, "Bạn chưa nhập mật khẩu mới", "Thông báo", JOptionPane.ERROR_MESSAGE);
            jPF_MKMoi_fThongTin.requestFocusInWindow();
            return;
        }
        if ("".equals(jPF_MKMoi2_fThongTin.getText())) {
            JOptionPane.showMessageDialog(this, "Bạn chưa nhập xác nhận mật khẩu", "Thông báo", JOptionPane.ERROR_MESSAGE);
            jPF_MKMoi2_fThongTin.requestFocusInWindow();
            return;
        }
        if (jPF_MKMoi_fThongTin.getPassword().length < 5) {
            JOptionPane.showMessageDialog(this, "Mật khẩu ít nhất 5 kí tự", "Thông báo", JOptionPane.ERROR_MESSAGE);
            jPF_MKMoi_fThongTin.setText("");
            jPF_MKMoi_fThongTin.requestFocusInWindow();
            return;
        }
        if (jPF_MKMoi2_fThongTin.getPassword().length < 5) {
            JOptionPane.showMessageDialog(this, "Mật khẩu ít nhất 5 kí tự", "Thông báo", JOptionPane.ERROR_MESSAGE);
            jPF_MKMoi2_fThongTin.setText("");
            jPF_MKMoi2_fThongTin.requestFocusInWindow();
            return;
        }
        String mkcu = frmLogin.encryptMD5(jPF_MKCu_fThongTin.getText());//String.valueOf(jPF_MKCu_fThongTin.getPassword());
        String mkmoi = jPF_MKMoi_fThongTin.getText();//String.valueOf(jPF_MKMoi_fThongTin.getPassword());
        String mkmoi2 = jPF_MKMoi2_fThongTin.getText();//String.valueOf(jPF_MKMoi2_fThongTin.getPassword());
        if (!frmLogin._us.getPassword().equals(mkcu)) {
            JOptionPane.showMessageDialog(this, "Bạn nhập sai mật khẩu cũ", "Thông báo", JOptionPane.ERROR_MESSAGE);
            jPF_MKCu_fThongTin.requestFocusInWindow();
            return;
        }
        if (!mkmoi.equals(mkmoi2)) {
            JOptionPane.showMessageDialog(this, "Mật khẩu xác nhận không trùng nhau", "Thông báo", JOptionPane.ERROR_MESSAGE);
            jPF_MKMoi_fThongTin.setText("");
            jPF_MKMoi2_fThongTin.setText("");
            jPF_MKMoi_fThongTin.requestFocusInWindow();
            return;
        }
        int result = JOptionPane.showConfirmDialog(this, "Bạn có chắc chắn muốn đổi mật khẩu", "Thông báo", JOptionPane.YES_NO_OPTION);
        if (result == JOptionPane.YES_OPTION) {
            try {
                UserDTO _us = new UserDTO();
                _us.setUserName(frmLogin._us.getUserName());
                WebRowSet wrs = UserController.user_GetById(_us);
                wrs.next();
                _us.setId(wrs.getObject("Id").toString());
                _us.setRole(frmLogin._us.getRole());
                _us.setPassword(frmLogin.encryptMD5(mkmoi));
                _us.setFullName(wrs.getObject("FullName").toString());
                UserController.user_Insert(_us);
            } catch (Exception e) {
                JOptionPane.showMessageDialog(new JFrame(), "Đường truyền bị gián đoạn ! Xin vui lòng kiểm tra lại", "Test Connect", JOptionPane.INFORMATION_MESSAGE);
            }
            JOptionPane.showMessageDialog(this, "Đổi mật khẩu thành công!", "Thông báo", JOptionPane.INFORMATION_MESSAGE);
            frmLogin._us.setPassword(frmLogin.encryptMD5(mkmoi));
            this.dispose();
        }
    }//GEN-LAST:event_jbcapnhatActionPerformed

    private void jPF_MKMoi2_fThongTinKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jPF_MKMoi2_fThongTinKeyTyped
        // TODO add your handling code here:
        if (jPF_MKMoi2_fThongTin.getPassword().length == 10) {
            getToolkit().beep();
            evt.consume();
        }
        if (!Character.isLetterOrDigit(evt.getKeyChar()) && !Character.isISOControl(evt.getKeyChar())) {
            getToolkit().beep();
            evt.consume();
        }
    }//GEN-LAST:event_jPF_MKMoi2_fThongTinKeyTyped

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new frmDoimatkhau().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel_Quyen_fThongTin;
    private javax.swing.JLabel jLabel_TK_fThongTin;
    private javax.swing.JPasswordField jPF_MKCu_fThongTin;
    private javax.swing.JPasswordField jPF_MKMoi2_fThongTin;
    private javax.swing.JPasswordField jPF_MKMoi_fThongTin;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JRadioButton jRB_Them_fNguoiDung;
    private javax.swing.JButton jbcapnhat;
    // End of variables declaration//GEN-END:variables
}
